package com.wuyan.web.form.service.async;

import com.wuyan.web.base.helper.rep.RepBody;
import com.wuyan.web.form.entity.PubMsg;
import com.wuyan.web.form.form.PubMsgForm;
import com.wuyan.web.form.service.api.WuyanPubMsgApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Future;

/**
 * 功能描述：异步任务业务类（@Async也可添加在方法上，@Async放在类上，表示类内方法皆为异步方法）
 */

@Component
@Async
@Slf4j
public class AsyncTaskService {

    @Autowired
    private WuyanPubMsgApiService wuyanPubMsgApiService;

    /**
     * 发送单个消息
     *
     * @param form 数据
     * @return Future<RepBody < PubMsg>>
     */
    public Future<RepBody<PubMsg>> sendMsg(PubMsgForm form) {
        RepBody<PubMsg> res = wuyanPubMsgApiService.create(form);
        return new AsyncResult<>(res);
    }

    /**
     * 批量发送消息
     *
     * @param forms 数据
     * @return Future<RepBody < List < PubMsg>>>
     */
    public Future<RepBody<List<PubMsg>>> sendMsg(List<PubMsgForm> forms) {
        RepBody<List<PubMsg>> res = wuyanPubMsgApiService.creates(forms);
        return new AsyncResult<>(res);
    }
}